/**
 * Copyright (C) 2013 The yydorid-framework project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.yangying.androidframework.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

/**
 * @author YANGYING
 *
 * @date 2011-8-11
 */
public class BitmapUtils {
    /*********************ͼ������**************************/
    public static Bitmap imageScale(Bitmap bitmap, int dst_w, int dst_h){
     
     int  src_w = bitmap.getWidth();
     int  src_h = bitmap.getHeight();
     float scale_w = ((float)dst_w)/src_w;
     float  scale_h = ((float)dst_h)/src_h;
     Matrix  matrix = new Matrix();
     matrix.postScale(scale_w, scale_h);
     Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix, true);
     
     return dstbmp;
    }
    
    /*********************DrawableתBitmap************************/
    public static Bitmap drawabletoBitmap(Drawable drawable){
     
     int width = drawable.getIntrinsicWidth();
     int height = drawable.getIntrinsicWidth();
     
     Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ?
        Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
     Canvas canvas = new Canvas(bitmap);
     drawable.setBounds(0, 0, width, height);
     
     drawable.draw(canvas);
     
     return bitmap;
    }
    
    /***************************ͼƬԲ�Ǵ���********************************/
    public static Bitmap getRCB(Bitmap bitmap, float roundPX) //RCB means Rounded Corner Bitmap
    {
     Bitmap dstbmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
     Canvas canvas = new Canvas(dstbmp);
     
     final int color = 0xff424242;
     final Paint paint = new Paint();
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
     final RectF rectF = new RectF(rect);
     paint.setAntiAlias(true);
     canvas.drawARGB(0, 0, 0, 0);
     paint.setColor(color);
     canvas.drawRoundRect(rectF, roundPX, roundPX, paint);
     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
     canvas.drawBitmap(bitmap, rect, rect, paint);

     return dstbmp;
    }
   }
